//
//  JHShare.m
//  JHShare
//
//  Created by mac on 2017/5/2.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "JZMShare.h"
#import <WebKit/WebKit.h>

@interface JZMShare()<UIWebViewDelegate,WKUIDelegate,WKNavigationDelegate>{
    BOOL isOK;
}
@property (nonatomic,strong,nonnull)UIWebView *myWebView;
@property (nonatomic,strong,nonnull)WKWebView *myWKWebView;

@end
JZMShare *web =nil;
@implementation JZMShare
+ (instancetype)shareInstance{
    if (!web) {
        web =[[[self class] alloc] init];
    }
    return web;
}

- (void)requestWithWebView:(UIWebView *)webView andUrl:(NSString *)uriString{
    self.myWebView =[UIWebView new];
    self.myWebView.delegate =web;
    [self.myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:uriString] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData timeoutInterval:10]];
    
}
- (BOOL)isOpenAppSpecialURLValue:(NSString *)string
{
    if ([string hasPrefix:@"http://"]) {
        return NO;
    } else if ([string hasPrefix:@"https://"]) {
        return NO;
    }
    return YES;
}
+ (void)openURL:(NSURL *)object complete:(void(^)(BOOL))complete
{
    UIApplication *application = nil;
    application = [UIApplication sharedApplication];
    SEL selector = @selector(openURL:options:completionHandler:);
    if ([UIApplication instancesRespondToSelector:selector])
    {
#ifdef __IPHONE_10_0
        [application openURL:object
                     options:[NSDictionary dictionary]
           completionHandler:complete];
#else
        if (complete) {
            complete([application openURL:object]);
        }
#endif
    }
    else
    {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
        if (complete) {
            complete([application openURL:object]);
        }
#pragma clang diagnostic pop
    }
}

- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(nonnull WKNavigationAction *)navigationAction decisionHandler:(nonnull void (^)(WKNavigationActionPolicy))decisionHandler{
    NSString *urlString = navigationAction.request.URL.absoluteString;
    if ([self isOpenAppSpecialURLValue:urlString]) {
        if ([urlString hasPrefix:@"about:blank"]||[urlString hasPrefix:@"jsbridge"]) {
            decisionHandler(WKNavigationActionPolicyAllow);
            return;
        }
        //非http和https开头的链接就使用OpenURL方法打开
        dispatch_async(dispatch_get_main_queue(), ^{
            [[self class] openURL:navigationAction.request.URL complete:^(BOOL status) {
                if (self.openComplete) {
                    self.openComplete(status);
                }
            }];
        });
        decisionHandler(WKNavigationActionPolicyAllow);
        
    }
    
    decisionHandler(WKNavigationActionPolicyAllow);
}
- (void)webView:(WKWebView *)webView decidePolicyForNavigationResponse:(WKNavigationResponse *)navigationResponse decisionHandler:(void (^)(WKNavigationResponsePolicy))decisionHandler{
    decisionHandler(WKNavigationResponsePolicyAllow);
    
}
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType{
    isOK =NO;
    NSString *absoluteString =request.URL.absoluteString;
    if ([self isOpenAppSpecialURLValue:absoluteString]) {
        if ([absoluteString hasPrefix:@"about:blank"]||[absoluteString hasPrefix:@"jsbridge"]) {
            return NO;
        }
        //非http和https开头的链接就使用OpenURL方法打开
        dispatch_async(dispatch_get_main_queue(), ^{
            [[self class] openURL:request.URL complete:^(BOOL status) {
                if (self.openComplete) {
                    self.openComplete(status);
                    self.myWebView.delegate = nil;
                }
            }];
        });
   
        return NO;
        
    }else{
        
        return YES;
    }
}
- (void)webViewDidFinishLoad:(UIWebView *)webView{
//    self.openComplete(isOK);
    [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"];
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的，原文没有提到。
    [[NSUserDefaults standardUserDefaults] synchronize];
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    
}
- (void)webView:(WKWebView *)webView didFailProvisionalNavigation:(null_unspecified WKNavigation *)navigation withError:(NSError *)error{
    NSLog(@"error=%@",error.userInfo);
}
@end
